#include <stdlib.h>
#include <string.h>


#include "ohos_init.h"
#include "cmsis_os2.h"
#include "esp_event.h"
#include "ethernet_basic.h"
#include "esp_netif_ip_addr.h"
#include "esp_netif_types.h"

#define TASK_STACK_SIZE         1024*4                //任务栈大小
#define TASK_PRIO               20 



void EthernetTask(void){
    int ret;
    esp_netif_ip_info_t* netif_ip_info = NULL;
	netif_ip_info = (esp_netif_ip_info_t*)malloc(sizeof(esp_netif_ip_info_t));
    memset(netif_ip_info, 0, sizeof(esp_netif_ip_info_t));

    ret = ethernet_basic();

    if(ret != ETH_SUCCESS){
        printf("eth connect fail\r\n");
    }else{
        printf("connect ethernet\r\n");
        osDelay(2*1000);
        ret = get_eth_ip(netif_ip_info);
        if (ret != ETH_SUCCESS){
            printf("get ip fail\r\n");
        }else{
            printf("Ethernet Got IP Address\r\n");
            printf("-------------\r\n");
            printf("ETHIP: %d.%d.%d.%d\r\n", IP2STR(&netif_ip_info->ip));
            printf("ETHMASK: %d.%d.%d.%d\r\n", IP2STR(&netif_ip_info->netmask));
            printf("ETHGW: %d.%d.%d.%d\r\n", IP2STR(&netif_ip_info->gw));
            printf("--------------\r\n");
        }
    }

    printf("******end\r\n");
    osThreadExit();
}

void EthernetDemo(void)
{
    osThreadAttr_t attr;

    attr.name = "TASK_NAME";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASK_STACK_SIZE;
    attr.priority = TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)EthernetTask, NULL, &attr) == NULL) {
        printf("Failed to create thread!\n");
    }
}

APP_FEATURE_INIT(EthernetDemo);